<?php

namespace App\Admin\Http\Controllers;

use App\Admin\Eloquent\GiftActivity;
use App\Admin\Eloquent\Project;
use App\Admin\Eloquent\Permission;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Route;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {   
        $this->__init();
    }

    /**
     * 初始化方法
     *
     * @return void
     */
    protected function __init()
    {
        $last_time=session('gift_update_time');
        if(time()-$last_time>60*60*4){
            session('gift_update_time',time());
            GiftActivity::updateActivity();
            Project::updateProject();
        }
    }

    /**
     * 根据控制器 action 生成重定向
     *
     * @param  string  $action
     * @param  array   $parameters
     * @param  int     $status
     * @param  array   $headers
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function redirectAction($action, $parameters = [], $status = 302, $headers = [])
    {
        $newAction = '\\App\\Admin\\Http\\Controllers\\' . $action;

        return redirect()->action($newAction, $parameters, $status, $headers);
    }

    /**
     * 当前用户
     *
     * @return App\Admin\User
     */
    protected function currentUser()
    {
        return session($this->getLoginUserSessionKey());
    }

    /**
     * 获取登录用户 SESSION KEY
     *
     * @return string
     */
    protected function getLoginUserSessionKey()
    {
        return sha1(config('session.admin_login_user'));
    }
}
